/*
 * 中北大学
 */
package com.zbdx.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zbdx.bean.Course;
import com.zbdx.bean.Score;
import com.zbdx.bean.Student;
import com.zbdx.dao.StudentDao;
import com.zbdx.util.DBUtil;

/**
 *
 * @author 王政
 * @version 1.0
 * 
 */
public class StudentDaoImpl implements StudentDao{

	public List<Student> findById(int Studentid){
		
		Connection conn = DBUtil.getConnection();
		PreparedStatement stmt = null;
		ResultSet rs = null;
		List<Student > list = new ArrayList<Student>();
		String sql ="SELECT * from Student WHERE StudentId=?";
		try {
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, Studentid);
			rs = stmt.executeQuery();
			while (rs.next()) {
				int StudentId = rs.getInt("StudentId");
				int CollegeId = rs.getInt("CollegeId");
				int SchoolId = rs.getInt("SchoolId");
				String StudentName = rs.getString("StudentName");
				String Password = rs.getString("Password");
				String IdCard = rs.getString("IdCard");
				String Sex = rs.getString("Sex");
				String Professional = rs.getString("Professional");
				String education = rs.getString("education");
				String acceptanceDate = rs.getString("acceptanceDate");
				String brither = rs.getString("brither");
				String tel = rs.getString("tel");
				
				Student student = new Student();
				
				student.setStudentId(StudentId);
				student.setCollegeId(CollegeId);
				student.setSchoolId(SchoolId);
				student.setStudentName(StudentName);
				student.setPassword(Password);
				student.setIdCard(IdCard);
				student.setSex(Sex);
				student.setProfessional(Professional);
				student.setEducation(education);
				student.setAcceptanceDate(acceptanceDate);
				student.setBrither(brither);
				student.setTel(tel);
				
				list.add(student);
				
				//System.out.println(list);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.closeResource(rs, stmt, conn);
		}
		

		return list;
		
	}

	@Override
	public int update(Student student) {
		// TODO Auto-generated method stub
		Connection conn  = DBUtil.getConnection();
		PreparedStatement  pstmt = null;
		int count = 0;
		String sql = "UPDATE student SET Password=?,Professional=?,Education=?,Tel=?, WHERE StudentName=?";
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, student.getPassword());
			pstmt.setString(2, student.getProfessional());
			pstmt.setString(3, student.getEducation());
			pstmt.setString(4, student.getTel());
			
			count = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.closeResource(null, pstmt, conn);
		}
		return count;
		
	}

//	@Override
//	public List<Student> findAll(int StudentId) {
//		// TODO Auto-generated method stub
//		
//		return null;
//	}

	@Override
	public boolean login(String StudentId, String Password) {
		// TODO Auto-generated method stub
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		boolean loginFlag=false;
		conn = DBUtil.getConnection();
		String sql = "SELECT*FROM Student WHERE StudentId=? AND Password=?";
		
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, StudentId);
			pstmt.setString(2, Password);
			rs = pstmt.executeQuery();
			
			if (rs.next()) {
				loginFlag = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBUtil.closeResource(rs, pstmt, conn);
		}
		return loginFlag;
		
		
	}

	@Override
	public List<Course> findcourse(int Courseid) {
		Connection conn = DBUtil.getConnection();
		PreparedStatement stmt = null;
		ResultSet rs = null;
		List<Course> list = new ArrayList<Course>();
		String sql ="SELECT * from Course  WHERE CourseId=?";
		try {
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, Courseid);
			rs = stmt.executeQuery();
			while (rs.next()) {
				int CourseId = rs.getInt("CourseId");
				int TeacherId = rs.getInt("TeacherId");
				String CourseName = rs.getString("CourseName");
				String CourseRoom = rs.getString("CouresRoom");
				int MaxStudentNum = rs.getInt("MaxStudentNum");
				int collageid = rs.getInt("collageld");
				Course course = new Course();
				course.setCourseId(CourseId);
				course.setTeacherId(TeacherId);
				course.setCourseName(CourseName);
				course.setCourseRoom(CourseRoom);
				course.setCollageId(collageid);
				list.add(course);
				//System.out.println(list);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.closeResource(rs, stmt, conn);
		}
		return list;
		
	
	}

	@Override
	public List<Score> findscore(int Scoreid) {
		// TODO Auto-generated method stub
		Connection conn = DBUtil.getConnection();
		PreparedStatement stmt = null;
		ResultSet rs = null;
		List<Score > list = new ArrayList<Score>();
		String sql ="SELECT * from sore  WHERE StudentId=?";
		try {
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, Scoreid);
			rs = stmt.executeQuery();
			while (rs.next()) {
				int ScoreId = rs.getInt("ScoreId");
				int StudentId = rs.getInt("StudentId");
				int CourseId = rs.getInt("CourseId");
				int Score = rs.getInt("Score");
				Score score = new Score();
				score.setScoreId(ScoreId);
				score.setStudentId(StudentId);
				score.setCourseId(CourseId);
				score.setScore(Score);
				list.add(score);
				
				//System.out.println(list);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.closeResource(rs, stmt, conn);
		}
		

		return list;
	}

}
